﻿using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace AutoCs.Screen
{
    public class ScreenCapture
    {

        public static void CaptureForm(IntPtr hWnd) {
            IntPtr hscrdc = CsSharpApi.GetWindowDC(hWnd);
            Rectangle rect = new Rectangle();
            CsSharpApi.GetWindowRect(hWnd, out rect);
            IntPtr hbitmap = CsSharpApi.CreateCompatibleBitmap(hscrdc, rect.Right - rect.Left, rect.Bottom - rect.Top);
            IntPtr hmemdc =CsSharpApi.CreateCompatibleDC(hscrdc);
            CsSharpApi.SelectObject(hmemdc, hbitmap);
            CsSharpApi.PrintWindow(hWnd, hmemdc, 0);
            Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
            CsSharpApi.DeleteDC(hscrdc);
            CsSharpApi.DeleteDC(hmemdc);
            string s = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cap.png");
            bmp.Save(s,ImageFormat.Png);
            Console.WriteLine("Save to " + s);
        }


    }
}
